//该js对封装的组件进行还原处理
$(function(){
	$initForm();
});

function $initForm(){
	$("durose").each(function(){
		var type= $(this).attr("type");
		if(type=='text'||type=='number'||type=='date'){
			$(this).parent().append($initInput(this));
			$(this).remove();
		}else if(type=='textarea'){
			$(this).parent().append($initTextArea(this));
			$(this).remove();
		}else if(type=='radio'){
			$(this).parent().append($initRadio(this));
			$(this).remove();
		}else if(type=='select'){
			$(this).parent().append($initSelect(this));
			$(this).remove();
		}else if(type=='readOnly'){
			$(this).parent().append($initDisabled(this));
			$(this).remove();
		}else if(type=='hidden'){
			$(this).parent().append($initHidden(this));
			$(this).remove();
		}else if(type=='primary'){
			$(this).parent().append($initHidden(this));
			$(this).remove();
		}else if(type=='picture'){
			$(this).parent().append($initPicture(this));
			$(this).remove();
		}
	});
}


function $initInput(obj){
	var $id = $(obj).attr("id");
	var $name = $(obj).attr("name");
	var $type = $(obj).attr("type");
	var $value=$(obj).attr("value");
	
	var htmlStr = '<div class="form-group row">';
	htmlStr+='<label  class="col-sm-2 col-form-label">'+$name+'</label>';
	htmlStr+='<div class="col-sm-10">';
	htmlStr+='<input class="form-control" value="'+$value+'"  id="'+$id+'" name="'+$id+'" type="'+$type+'">';
	htmlStr+='</div></div>';
	return htmlStr;
}

function $initDisabled(obj){
	var $id = $(obj).attr("id");
	var $name = $(obj).attr("name");
	var $value=$(obj).attr("value");
	
	var htmlStr = '<div class="form-group row">';
	htmlStr+='<label  class="col-sm-2 col-form-label">'+$name+'</label>';
	htmlStr+='<div class="col-sm-10">';
	htmlStr+='<input class="form-control" value="'+$value+'"  id="'+$id+'" name="'+$id+'" type="text" readOnly="readOnly">';
	htmlStr+='</div></div>';
	return htmlStr;
}

function $initHidden(obj){
	var $id = $(obj).attr("id");
	var $type = $(obj).attr("type");
	var $value=$(obj).attr("value");
	var htmlStr = '<input class="form-control" value="'+$value+'"  id="'+$id+'" name="'+$id+'" type="hidden">';
	return htmlStr;
}

function $initTextArea(obj){
	var $id = $(obj).attr("id");
	var $name = $(obj).attr("name");
	var $value=$(obj).attr("value");
	
	var htmlStr = '<div class="form-group row">';
	htmlStr+='<label  class="col-sm-2 col-form-label">'+$name+'</label>';
	htmlStr+='<div class="col-sm-10">';
	htmlStr+='<textArea class="form-control" value="'+$value+'"  id="'+$id+'" name="'+$id+'">'+$value+'</textArea>';
	htmlStr+='</div></div>';
	return htmlStr;
}

function $initRadio(obj){
	var $id = $(obj).attr("id");
	var $value=$(obj).attr("extraValue").split("$");
	$value.pop();
	var $name = $(obj).attr("name");
	var $valueSelected=$(obj).attr("value");
	
	var htmlStr = '<div class="form-group row">';
	htmlStr+='<label  class="col-sm-2 col-form-label">'+$name+'</label>';
	htmlStr+='<div class="col-sm-10">';
	for(i in $value){
		if($valueSelected==$value[i]){
			htmlStr+='<input  value="'+$value[i]+'"  id="'+$id+'" name="'+$id+'" type="radio" checked="checked">'+$value[i]+'&nbsp;';
		}else{
			htmlStr+='<input  value="'+$value[i]+'"  id="'+$id+'" name="'+$id+'" type="radio">'+$value[i]+'&nbsp;';
		}
	}
	htmlStr+='</div></div>';
	return htmlStr;
}

function $initPicture(obj){
	var $id = $(obj).attr("id");
	var $name = $(obj).attr("name");
	var $value=$(obj).attr("value");
	
	var htmlStr = '<div class="form-group row">';
	htmlStr+='<label  class="col-sm-2 col-form-label">'+$name+'</label>';
	htmlStr+='<div class="col-sm-10">';
	if($value==""||$value==undefined){
		htmlStr+='<img class="img-thumbnail uploadImg" alt="200x200" style="width: 128px; height: 128px;" src="static/images/users/avatar-1.jpg" >';
	}else{
		htmlStr+='<img class="img-thumbnail uploadImg" alt="200x200" style="width: 128px; height: 128px;" src="static/img/'+$value+'" >';
	}
	htmlStr+='<input class="form-control uploadBtn" id="'+$id+'" value="" accept="image/*" onchange="uploadImg(&apos;'+$id+'&apos;)" type="file">';
	htmlStr+='<input class="form-control"  name="'+$id+'" value="'+$value+'" type="hidden">';
	htmlStr+='</div></div>';
	return htmlStr;
}

function $initSelect(obj){
	//判断下拉框是否存在着外键关联
	if($(obj).attr("foreginValue")==undefined){
		var $id = $(obj).attr("id");
		var $value=$(obj).attr("extraValue").split("$");
		$value.pop();
		var $name = $(obj).attr("name");
		var $valueSelected=$(obj).attr("value");
		
		var htmlStr = '<div class="form-group row">';
		htmlStr+='<label  class="col-sm-2 col-form-label">'+$name+'</label>';
		htmlStr+='<div class="col-sm-10">';
		htmlStr+='  <select class="custom-select" name="'+$id+'">';
		for(i in $value){
			if($valueSelected==$value[i]){
				htmlStr+='<option  value="'+$value[i]+'"   selected=true>'+$value[i]+'</option>';
			}else{
				htmlStr+='<option  value="'+$value[i]+'"   >'+$value[i]+'</option>';
			}
		}
		htmlStr+='</select></div></div>';
		return htmlStr;
	}else{
		var $id = $(obj).attr("id");
		var $value=$(obj).attr("extraValue").split("$");
		$value.pop();
		var $name = $(obj).attr("name");
		var $valueSelected=$(obj).attr("value");
		var $foreginValue=$(obj).attr("foreginValue").split("$");
		
		var htmlStr = '<div class="form-group row">';
		htmlStr+='<label  class="col-sm-2 col-form-label">'+$name+'</label>';
		htmlStr+='<div class="col-sm-10">';
		htmlStr+='  <select class="custom-select" name="'+$id+'">';
		for(i in $value){
			if($valueSelected==$value[i]){
				htmlStr+='<option  value="'+$value[i]+'"  selected=true>'+$foreginValue[i]+'</option>';
			}else{
				htmlStr+='<option  value="'+$value[i]+'"  >'+$foreginValue[i]+'</option>';
			}
		}
		htmlStr+='</select></div></div>';
		
		return htmlStr;
	}
}

function $initTable(url,pageNumber){
	$(".pagination").empty();
	var queryName=$("#queryName").val();
	$.ajax({
		url:url,
		data:{queryName:queryName,pageNumber:pageNumber},
		type:"post",
		success:function(data){
			var htmlStr = ' <li class="page-item"><a class="page-link" href="javaScript:void(0)" onclick="$initTable(&apos;'+url+'&apos;,0)">首页</a></li>';
				htmlStr += ' <li class="page-item"><a class="page-link" href="javaScript:void(0)" onclick="$initTable(&apos;'+url+'&apos;,'+(data.pageNum-1)+')">上一页</a></li>';
				htmlStr += ' <li class="page-item active"><a class="page-link" href="javaScript:void(0)" onclick="$initTable(&apos;'+url+'&apos;,'+data.pageNum+')">'+data.pageNum+'</a></li>';
				htmlStr += ' <li class="page-item"><a class="page-link" href="javaScript:void(0)" onclick="$initTable(&apos;'+url+'&apos;,'+(data.pageNum+1)+')">下一页</a></li>';
				htmlStr += ' <li class="page-item"><a class="page-link" href="javaScript:void(0)" onclick="$initTable(&apos;'+url+'&apos;,'+data.pages+')">尾页</a></li>';
				$(".pagination").html(htmlStr);
				initMyTable(data.list);
		}
	});
}
//数据提交操作，针对idForm下的数据访问url进行存储，如果访问成功会关闭idModal的模态框,并且重新绘制表格
function $submit(url,urlSuccess,idForm,idModal){
	var $idForm = "#"+idForm;
	var $idModal = "#"+idModal;
	var data = $($idForm).serialize();
	$.ajax({
		url:url,
		type:"post",
		data:data,
		success:function(data){
			if(data.status=="OK"){
				alert("操作成功！");
				$($idModal).modal("hide");
				//绘制表格
				$initTable(urlSuccess,1);
			}else{
				alert("操作失败！");
			}
		},
		error:function(data){
			alert("操作失败！");
		}
	});
}

//删除操作
function $delete(url,urlSuccess,id){
	var $url = url+"?id="+id;
	var a = confirm("确认要删除?");
	if(a){
		$.ajax({
			url:$url,
			success:function(data){
				if(data.status=="OK"){
					alert("删除成功！");
					$initTable(urlSuccess,1);
				}
			}
		});
	}
			
}